package com.jeeplus.invoice.api;

import com.jeeplus.invoice.customer.service.YxfpCustomerService;
import com.jeeplus.invoice.customer.service.dto.YxfpCustomerDTO;
import com.jeeplus.invoice.customer.service.mapstruct.YxfpCustomerWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "越香发票-客户管理API")
@RestController
@RequestMapping(value = "/invoice/api/customer")
public class CustomerAPI {

    @Autowired
    private YxfpCustomerService yxfpCustomerService;

    @Autowired
    private YxfpCustomerWrapper yxfpCustomerWrapper;

    /**
     * 根据Id获取客户数据
     */
    @ApiOperation(value = "根据Id获取客户数据")
    @GetMapping("getCustomerById")
    public ResponseEntity<YxfpCustomerDTO> getCustomerById(String id) {
        return ResponseEntity.ok(yxfpCustomerService.findById(id));
    }

    /**
     * 保存客户数据
     */
    @ApiOperation(value = "保存客户数据")
    @PostMapping("saveCustomer")
    public  ResponseEntity <String> saveCustomer(@Valid @RequestBody YxfpCustomerDTO yxfpCustomerDTO) {
        //新增或编辑表单保存
        yxfpCustomerService.saveOrUpdate(yxfpCustomerWrapper.toEntity(yxfpCustomerDTO));
        return ResponseEntity.ok("保存客户数据成功");
    }

    /**
     * 通过微信OpenID获取客户信息
     * @param openId
     */
    @ApiOperation("通过客户微信OpenID获取客户信息")
    @GetMapping("queryByOpenId")
    public ResponseEntity<YxfpCustomerDTO> getCustomerByOpenId(String openId){
        return ResponseEntity.ok(yxfpCustomerService.findByOpenId(openId));
    }

}
